<?php
/////////////////////////////////////////////////////////////////////////////////////////
//
// GET QUESTION FROM DB                                                           
//
/////////////////////////////////////////////////////////////////////////////////////////
function getQuestion() {
	$db = mysqli_connect ("localhost", "lr12aas", "u3CJFB4r", "dblr12aas");
		if (mysqli_connect_errno()) {
			printf ("Connect failed: %s <br />",
				mysqli_connect_error());
					//echo "<p class='crying'>Connection problem</p>";
						exit ();
							}
else {
	// echo "<p class='smiling'>Connected</p>";			
		}
$query= "SELECT ItemID, stem, option1, option2, option3, option4 FROM item WHERE " . $_SERVER['QUERY_STRING'] . "";
	$result = mysqli_query($db, $query);
		if (!$result) {
			return false;
				}
//check for rows
	$numberOfRows = mysqli_num_rows($result);
		if ($numberOfRows > 0) {
			$fetchr= array();
//get stuff out of the row
	while ($row = mysqli_fetch_array($result)) {
		$fetchr[] = $row;
			}
			return $fetchr;
				} 		
					}   
/////////////////////////////////////////////////////////////////////////////////////////
//
// GET ANSWER FROM DB                                                           
//
/////////////////////////////////////////////////////////////////////////////////////////
function putAnswer(){
//SET ALL ITEMS TO ZERO SO THAT NONE ARE SELECTED
$option_checked = '';
$option1_status = 0;
$option2_status = 0;
$option3_status = 0;
$option4_status = 0;

// SEE IF THE FORM WAS SUBMITTED
if (isset($_POST['submitAnswer'])) {

	//SELECTED ANSWER IS THE SELECTED RADIO FROM THE GROUP 'questionOptions'
	$selectedAnswer = $_POST['questionOptions'];
	
		//THEN SEE IF A RADIO BUTTON WAS SELECTED
			if (isset($_POST['questionOptions'])){
			
			// IF SO UPDATE STATUS TO 1
				if ($selectedAnswer == 'option1') {
				$option1_status = 1;
				}
				if ($selectedAnswer == 'option2') {
					$option2_status = 1;
					}
					if ($selectedAnswer == 'option3') {
						$option3_status = 1;
						}
						if ($selectedAnswer == 'option4') {
							$option4_status = 1;
							}
								//	echo "<p class='smiling'>Selected Answer: " . $selectedAnswer . "</p>";	
						}
//IF THE FORM HAS NOT BEEN SUBMITTED, HIDE questionOptions
else {
	$questionOptions = null;	
		//TELL THE LEARNER TO SELECT AN ANSWER
		echo "<p class='crying'>Please select an answer</p>";
	}
$db = mysqli_connect ("localhost", "lr12aas", "u3CJFB4r", "dblr12aas");
	if (mysqli_connect_errno()) {
		printf ("Connect failed: %s <br />",
			mysqli_connect_error());
				//echo "<p class='crying'>Connection problem to student response</p>";
				exit ();
			}
				else {
				//echo "<p class='smiling'>Connected to studentresponse</p>";			
			}
				$query = "INSERT INTO studentresponse1 (Option1Selected, Option2Selected, Option3Selected, 		Option4Selected, ItemID) VALUES ("
			. $option1_status . ", "
			. $option2_status . ", "
			. $option3_status . ", "
			. $option4_status . ", "
			.  $_GET['ItemID'] .")";	 
				$result = mysqli_query($db, $query);
					}
				}
/////////////////////////////////////////////////////////////////////////////////////////
//
// GET CORRECT ANSWER TO PROVIDE FEEDBACK                                                         
//
/////////////////////////////////////////////////////////////////////////////////////////
function getCorrect() {
		$selectedAnswer = $_POST['questionOptions'];
$db = mysqli_connect ("localhost", "lr12aas", "u3CJFB4r", "dblr12aas");
   		if (mysqli_connect_errno()) {
				printf ("Connect failed: %s <br />",
					mysqli_connect_error());
						//echo "<p class='crying'>Connection problem getting correct answer</p>";
							exit ();
								}
									else {
										//echo "<p class='smiling'>Connected to correct answer</p>";			
											}
												//GET ALL THE ANSWERS FROM DB
						$query= "SELECT IsKey1, IsKey2, IsKey3, IsKey4 FROM item WHERE " . $_SERVER['QUERY_STRING'] . "";
	 				$result = mysqli_query($db, $query);
			$row=mysqli_fetch_array($result,MYSQLI_NUM);
		$arrlength=count($row);

foreach ($row as $value)
{
	echo $value;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Multiple Choice Assessment</title>
<link href="styles/styles.css" rel="stylesheet" type="text/css">
<script src="scripts/scripts.js" type="text/javascript"></script>
</head>
<body>
<?php

//RUN FUNCTIONS  
$item = getQuestion();
$putAnswer = putAnswer();
$getCorrect = getCorrect();

//CREATE VARIABLES FOR QUESTION DATA
if ($item) {
$itemID = $item[0][0];
$itemStem = $item[0][1];
$option1 = $item[0][2];
$option2 = $item[0][3];
$option3 = $item[0][4];
$option4 = $item[0][5];
} 
else {
	echo "<p class='crying'>Could not get question</p>";			
		}
?>
<div id="container">
  <header>
    <h1>Multiple Choice Assessment</h1>
  </header>
  <section id="mainContent">
    <?php if (isset($_POST['submitAnswer'])) { $selectedAnswer = $_POST['questionOptions'];}?>
    <form id="MC-1" action="" method="POST" class="question">
      <div class="q-stem">
        <p id="itemID" class="hidden"><?php echo $itemID ?></p>
        <p id="itemStem"><?php echo $itemStem ?></p>
      </div>
      <div class="q-option">
        <input name="questionOptions" id="radio1" type="radio" value="option1" class="q-input" <?php if ($selectedAnswer == 'option1') echo ' checked="checked"';?>>
        <label id="option1Lbl" for="radio1" class="form-lbl"> <?php echo $option1 ?></label>
      </div>
      <div class="q-option">
        <input name="questionOptions" id="radio2" type="radio" value="option2" class="q-input"<?php if ($selectedAnswer == 'option2') echo ' checked="checked"';?>>
        <label id="option1Lbl2" for="radio2" class="form-lbl"> <?php echo $option2 ?></label>
      </div>
      <div class="q-option">
        <input name="questionOptions" id="radio3" type="radio" value="option3" class="q-input"<?php if ($selectedAnswer == 'option3') echo ' checked="checked"';?>>
        <label id="option1Lbl3" for="radio3" class="form-lbl"> <?php echo $option3 ?></label>
      </div>
      <div class="q-option">
        <input name="questionOptions" id="radio4" type="radio" value="option4" class="q-input"<?php if ($selectedAnswer == 'option4') echo ' checked="checked"';?>>
        <label id="option1Lbl4" for="radio4" class="form-lbl"> <?php echo $option4 ?></label>
      </div>
      <input name="submitAnswer" id="submitAnswer" type="submit" value="Check Answer" class="q-submit">
    </form>
    <?php
if (isset($_POST['questionOptions'])){
		echo "<p class='smiling'>Selected Answer: " . $selectedAnswer . "</p>";	
		echo "<div class='feedback'><p>";
		echo "some feedback";
		//echo $feedback;
		echo "</p></div>";	  
}
	?>
  </section>
  <footer></footer>
</div>
</body>
</html>
